#include "uart.h"
#include "../../sensor_check/sensor.h"

/** UART状态
 * 0 -> not start
 * 1 -> idle            (Only read until connect)
 * 2 -> wait linked     (active request)
 * 3 -> linked          (Good)
 * 4 -> rx_buffer full
 * 5 -> timer error
 */
uint8_t Serial_uart::state = 0b0;
int32_t Serial_uart::uart_speed = DEFAULT_SERIAL_BAUD; // 9600

static const char Check_code[] = UART_CHECK_CODE;
static const uint8_t size = sizeof(Check_code);
millis_t Serial_uart::serial_send_time = millis();
millis_t Serial_uart::serial_delay = 0; // 存放串口delay时间

static const char call_ok[3] = "OK";

// 延迟计算
inline void _update_serial_delay()
{
    if (uart.uart_speed == 0)
    {
        uart.send("ERROR:UART_SPEED ERROR");
        return;
    }

    float delay = (float)uart.uart_speed / SERIAL_SEND_PACKET;
    if (delay < 0.99f)
        uart.serial_delay = 0;
    else
        uart.serial_delay = millis_t(((float)SERIAL_SEND_PACKET / (float)uart.uart_speed) * 1000) + (millis_t)SERIAL_SEND_DELAY;
    uart.send("INFO:SERIAL_DELAY:", 0);
    uart.send(uart.serial_delay);
}

void Serial_uart::send(const String call = call_ok, const uint8_t enter)
{
    if (enter)
        Serial0.println(call);
    else
        Serial0.print(call);
}

// uint
void Serial_uart::send(const int32_t call, const uint8_t enter)
{
    if (enter)
        Serial0.println(call);
    else
        Serial0.print(call);
}

void Serial_uart::init(bool idle)
{
    Serial0.begin(uart_speed);
    Serial0.setTimeout(0);
    Serial0.setTxBufferSize(128);
    _update_serial_delay();
    state = idle ? 0b1 : 0b10;
}

void Serial_uart::change_speed(int32_t speed)
{
    Serial0.updateBaudRate(speed);
    _update_serial_delay();
}

void Serial_uart::set_sensitivity(int32_t point, int32_t value)
{
    if (point > 0 && point <= TOUCH_NUM)
        if (value >= 0 && value <= 15)
        {
            sensor.touch_refence_update(true);
            sensor.touch_spl_set(point - 1, value);
            uart.send(" ");
            uart.send("INFO: SET ", 0);
            uart.send(point, 0);
            uart.send("S:", 0);
            uart.send(value, 0);
            sensor.touch_refence_update(false);
            uart.send(" OK");
            sensor.tsm12mc_reset();
            vTaskDelay(100);
            sensor.start_sample();
            return;
        }
    uart.send("ERROR: point or value error");
}

void Serial_uart::set_pressure(int32_t point, int32_t value)
{
    if (point > 0 && point <= TOUCH_NUM)
        if (value >= 0 && value <= 2)
        {
            sensor.touch_pressure_set(point - 1, value);
            uart.send(" ");
            uart.send("INFO: SET ", 0);
            uart.send(point, 0);
            uart.send("P:", 0);
            uart.send(value, 0);
            uart.send(" OK");
            return;
        }
    uart.send("ERROR: point or value error");
}

// IDLE
void Serial_uart::idle()
{

    char buffer[16];

    Serial0.read(buffer, 16);

    uint8_t count = 0;
    while (count > size)
    {
        if (buffer[count] != 0xFF && buffer[count] != 0x00)
        {
            uint8_t check_count = 0;
            while (size > check_count)
            {
                if (buffer[count + check_count] == Check_code[check_count])
                {
                    check_count++;
                }
                else
                    break;
            }
        }
        count++;
        vTaskDelay(1);
    }
}